package com.woniuxy.advice;

import com.woniuxy.config.RedisConfig;
import com.woniuxy.service.UserService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class AdviceConfig {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisConfig redisConfig;

    @Before("@annotation(com.woniuxy.annotation.MyAnnotation)")
    public void getRegisterCode(JoinPoint jp) throws Exception {
        Object[] args = jp.getArgs();
        Object obj = args[0];
        Class<?> clazz = obj.getClass();
        String tel = (String)clazz.getDeclaredMethod("getTel").invoke(obj);
        String code = (String)clazz.getDeclaredMethod("getCode").invoke(obj);
        String codeT = (String)redisConfig.opsForValue().get("woniuxy:" + tel);
        if(codeT==null)throw new RuntimeException("验证码过期");
        if(code==null||tel==null)throw new RuntimeException("没有验证码");
        if(!code.equals(codeT))throw new RuntimeException("验证码不正确");
    }
}
